package cn.edu.scau.cmi.ema.dao;

import java.util.Arrays;
import java.util.HashSet;
import java.util.LinkedHashSet;
import java.util.Set;

import javax.persistence.EntityManager;
import javax.persistence.NoResultException;
import javax.persistence.PersistenceContext;
import javax.persistence.Query;

import org.springframework.dao.DataAccessException;

import org.springframework.stereotype.Repository;

import org.springframework.transaction.annotation.Transactional;

import org.hibernate.criterion.DetachedCriteria;
import org.hibernate.Session;


import cn.edu.scau.cmi.ema.domain.*;
import cn.edu.scau.cmi.ema.domain.base.CmiPagination;


import cn.edu.scau.cmi.ema.util.CmiSqlUtil;

@Repository("FoodbatchmaterialDAO")
@Transactional
public class FoodbatchmaterialDAOImpl extends AbstractJpaDao<Foodbatchmaterial> implements FoodbatchmaterialDAO {

	private final static Set<Class<?>> dataTypes = new HashSet<Class<?>>(
			Arrays.asList(new Class<?>[] { Foodbatchmaterial.class }));

	@PersistenceContext(unitName = "ema")
	private EntityManager entityManager;

	public FoodbatchmaterialDAOImpl() {
		super();
	}

	public EntityManager getEntityManager() {
		return entityManager;
	}

	public Set<Class<?>> getTypes() {
		return dataTypes;
	}

	@Transactional
	public Foodbatchmaterial findFoodbatchmaterialById(Integer id) throws DataAccessException {

		return findFoodbatchmaterialById(id, -1, -1);
	}

	@Transactional
	public Foodbatchmaterial findFoodbatchmaterialById(Integer id, int startResult, int maxRows)
			throws DataAccessException {
		try {
			Query query = createNamedQuery("findFoodbatchmaterialById", startResult, maxRows, id);
			return (Foodbatchmaterial) query.getSingleResult();
		} catch (NoResultException nre) {
			return null;
		}
	}

	@Transactional
	public Set<Foodbatchmaterial> findFoodbatchmaterialByName(String name) throws DataAccessException {

		return findFoodbatchmaterialByName(name, -1, -1);
	}

	@SuppressWarnings("unchecked")
	@Transactional
	public Set<Foodbatchmaterial> findFoodbatchmaterialByName(String name, int startResult, int maxRows)
			throws DataAccessException {
		Query query = createNamedQuery("findFoodbatchmaterialByName", startResult, maxRows, name);
		return new LinkedHashSet<Foodbatchmaterial>(query.getResultList());
	}

	@Transactional
	public Set<Foodbatchmaterial> findFoodbatchmaterialByNameContaining(String name) throws DataAccessException {

		return findFoodbatchmaterialByNameContaining(name, -1, -1);
	}

	@SuppressWarnings("unchecked")
	@Transactional
	public Set<Foodbatchmaterial> findFoodbatchmaterialByNameContaining(String name, int startResult, int maxRows)
			throws DataAccessException {
		Query query = createNamedQuery("findFoodbatchmaterialByNameContaining", startResult, maxRows, name);
		return new LinkedHashSet<Foodbatchmaterial>(query.getResultList());
	}

	@Transactional
	public Set<Foodbatchmaterial> findFoodbatchmaterialByContent(String content) throws DataAccessException {

		return findFoodbatchmaterialByContent(content, -1, -1);
	}

	@SuppressWarnings("unchecked")
	@Transactional
	public Set<Foodbatchmaterial> findFoodbatchmaterialByContent(String content, int startResult, int maxRows)
			throws DataAccessException {
		Query query = createNamedQuery("findFoodbatchmaterialByContent", startResult, maxRows, content);
		return new LinkedHashSet<Foodbatchmaterial>(query.getResultList());
	}

	@Transactional
	public Set<Foodbatchmaterial> findFoodbatchmaterialByContentContaining(String content) throws DataAccessException {

		return findFoodbatchmaterialByContentContaining(content, -1, -1);
	}

	@SuppressWarnings("unchecked")
	@Transactional
	public Set<Foodbatchmaterial> findFoodbatchmaterialByContentContaining(String content, int startResult, int maxRows)
			throws DataAccessException {
		Query query = createNamedQuery("findFoodbatchmaterialByContentContaining", startResult, maxRows, content);
		return new LinkedHashSet<Foodbatchmaterial>(query.getResultList());
	}

	@Transactional
	public Set<Foodbatchmaterial> findFoodbatchmaterialByDescription(String description) throws DataAccessException {

		return findFoodbatchmaterialByDescription(description, -1, -1);
	}

	@SuppressWarnings("unchecked")
	@Transactional
	public Set<Foodbatchmaterial> findFoodbatchmaterialByDescription(String description, int startResult, int maxRows)
			throws DataAccessException {
		Query query = createNamedQuery("findFoodbatchmaterialByDescription", startResult, maxRows, description);
		return new LinkedHashSet<Foodbatchmaterial>(query.getResultList());
	}

	@Transactional
	public Set<Foodbatchmaterial> findFoodbatchmaterialByDescriptionContaining(String description)
			throws DataAccessException {

		return findFoodbatchmaterialByDescriptionContaining(description, -1, -1);
	}

	@SuppressWarnings("unchecked")
	@Transactional
	public Set<Foodbatchmaterial> findFoodbatchmaterialByDescriptionContaining(String description, int startResult,
			int maxRows) throws DataAccessException {
		Query query = createNamedQuery("findFoodbatchmaterialByDescriptionContaining", startResult, maxRows,
				description);
		return new LinkedHashSet<Foodbatchmaterial>(query.getResultList());
	}

	@Transactional
	public Foodbatchmaterial findFoodbatchmaterialByPrimaryKey(Integer id) throws DataAccessException {
		return findFoodbatchmaterialByPrimaryKey(id, -1, -1);
	}

	@Transactional
	public Foodbatchmaterial findFoodbatchmaterialByPrimaryKey(Integer id, int startResult, int maxRows)
			throws DataAccessException {
		try {
			Query query = createNamedQuery("findFoodbatchmaterialByPrimaryKey", startResult, maxRows, id);
			return (Foodbatchmaterial) query.getSingleResult();
		} catch (NoResultException nre) {
			return null;
		}
	}

	@Transactional
	public Set<Foodbatchmaterial> findFoodbatchmaterialsByIds(Set<Integer> ids) throws DataAccessException {
		try {
			Query query = createNamedQuery("findFoodbatchmaterialsByIds", -1, -1, ids);
			return new HashSet(query.getResultList());
		} catch (NoResultException nre) {
			return null;
		}
	}

	@Transactional
	public Set<Foodbatchmaterial> findAllFoodbatchmaterials() throws DataAccessException {

		return findAllFoodbatchmaterials(-1, -1);
	}

	@SuppressWarnings("unchecked")
	@Transactional
	public Set<Foodbatchmaterial> findAllFoodbatchmaterials(int startResult, int maxRows) throws DataAccessException {
		Query query = createNamedQuery("findAllFoodbatchmaterials", startResult, maxRows);
		return new LinkedHashSet<Foodbatchmaterial>(query.getResultList());
	}

	@SuppressWarnings("unchecked")
	public Set<Foodbatchmaterial> findFoodbatchmaterialsByFoodbatch(Integer foodbatchId, int start, int max)
			throws DataAccessException {
		Query query = createNamedQuery("findAllFoodbatchmaterialsByFoodbatch", start, max, foodbatchId);
		return new LinkedHashSet<Foodbatchmaterial>(query.getResultList());
	}

	@SuppressWarnings("unchecked")
	public Set<Foodbatchmaterial> findFoodbatchmaterialsByMaterial(Integer materialId, int start, int max)
			throws DataAccessException {
		Query query = createNamedQuery("findAllFoodbatchmaterialsByMaterial", start, max, materialId);
		return new LinkedHashSet<Foodbatchmaterial>(query.getResultList());
	}

	@SuppressWarnings("unchecked")
	@Transactional
	public Set<Foodbatchmaterial> findFoodbatchmaterialByCompositeProperities(String sql, int startResult,
			int maxRows) {
		return new LinkedHashSet<Foodbatchmaterial>(createQuery(sql, startResult, maxRows).getResultList());
	}

	@Transactional
	public Set<Foodbatchmaterial> findFoodbatchmaterialsByDetachedCriteria(CmiPagination page)
			throws DataAccessException {
		DetachedCriteria detachedCriteria = CmiSqlUtil.createDetachedCriteria(page, Foodbatchmaterial.class);
		Session session = entityManager.unwrap(Session.class);
		Set<Foodbatchmaterial> foodbatchmaterials = new HashSet(detachedCriteria.getExecutableCriteria(session)
				.setFirstResult(page.getSearchBeginCursor()).setMaxResults(page.getPaginationSize()).list());

		return foodbatchmaterials;
	}

	@Transactional
	public Foodbatchmaterial findFoodbatchmaterialByPhysicalPrimaryKey(Integer foodbatchId, Integer materialId,
			String content) throws DataAccessException {
		return findFoodbatchmaterialByPhysicalPrimaryKey(foodbatchId, materialId, content, -1, -1);
	}

	@Transactional
	public Foodbatchmaterial findFoodbatchmaterialByPhysicalPrimaryKey(Integer foodbatchId, Integer materialId,
			String content, int startResult, int maxRows) throws DataAccessException {
		try {
			Query query = createNamedQuery("findFoodbatchmaterialByPhyscialPrimaryKey", startResult, maxRows,
					foodbatchId, materialId, content);
			return (Foodbatchmaterial) query.getSingleResult();
		} catch (NoResultException nre) {
			return null;
		}
	}

	public boolean canBeMerged(Foodbatchmaterial entity) {
		return true;
	}
}
